package reduce.fz.com.reduce.activity;

import android.app.Service;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import cn.sharesdk.framework.ShareSDK;
import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.TeamSubject;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.PubSubjectEvent;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class TeamSubjectCreateActivity extends BaseToolBarActivity implements View.OnClickListener {

    private EditText mSubjectDesc;
    private TextView msubmit;
    private TeamSubject subject;
    private Long teamid;
    private TeamService teamService= ServiceBuilder.builderTeamService(null);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_subject_create);
        setToolbarTitle("话题创建");
        initWidget();
        teamid=getIntent().getLongExtra("teamid",0);
    }

    @Override
    public void initWidget() {
        mSubjectDesc = (EditText) findViewById(R.id.subject_desc);
        msubmit = (TextView) findViewById(R.id.tv_submit);
        msubmit.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    public void reply()
    {
        String desc=mSubjectDesc.getText().toString();
        if(TextUtils.isEmpty(desc)) {
            toastMsg("请输入话题描述");
            InputMethodUtil.reuestFocusAndShowInput(msubmit);
        }
        subject=new TeamSubject();
        subject.teamid=teamid;
        subject.description=desc;
        subject.uid= UserPreference.getUser().uid;
        final LoadingDialog dialog=new LoadingDialog(this,"提交中");
        dialog.show();
        teamService.pubSubject(subject, new AbResponseListener<TeamSubject>(new TypeReference<Result<TeamSubject>>(){}) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();
            }
            @Override
            public void responseInfo(Result<TeamSubject> data) {
                dialog.dismiss();
                EventBus.getDefault().post(new PubSubjectEvent(data.getData()));
                finish();

            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_submit:
                reply();
                break;
            default:
                break;
        }

    }
}
